"""
导出翻译数据库，用于以下页面更新：
https://wiki.biligame.com/umamusume/%E4%B8%AD%E6%97%A5%E6%96%87%E5%AF%B9%E6%AF%94%E8%A1%A8
https://wiki.biligame.com/umamusume/%E6%A8%A1%E5%9D%97:%E7%BF%BB%E8%AF%91%E6%95%B0%E6%8D%AE%E5%BA%93
"""
import sqlite3

# 日服数据库
con_JP = sqlite3.connect("database/master_JP.mdb")
cur_JP = con_JP.cursor()
# 繁中数据库
con_TW  = sqlite3.connect("database/master_TW.mdb")
cur_TW = con_TW.cursor()
# 简中数据库
con_CN  = sqlite3.connect("database/master_CN.mdb")
cur_CN = con_CN.cursor()
# 输出文件
file = open("export_data/translate.txt","w",encoding='utf-8')
file.write("local p={}\n")

def getText(category):
    file.write("p.text_data_" + str(category) + "={\n")
    cur_JP.execute("select * from text_data where category = %d" %category)
    is_first = True
    for data_JP in cur_JP:
        if is_first:
            is_first = False
        else:
            file.write(",\n")
        cur_TW.execute("select * from text_data where category = %d" % category)
        cur_CN.execute("select * from text_data where category = %d" % category)
        text_JP = data_JP[3].replace("\\n", "<br>")
        text_TW =""
        text_CN = ""
        for data_TW in cur_TW:
            if data_JP[2] == data_TW[2]:
                text_TW = data_TW[3].replace("\\n", "<br>")
        for data_CN in cur_CN:
            if data_JP[2] == data_CN[2]:
                 text_CN = data_CN[3].replace("\\n", "<br>")
        file.write("{id=\""+str(data_JP[0])+"\",category=\""+str(data_JP[1])+"\",index=\""+str(data_JP[2])+"\",text_JP=\""+text_JP+
                   "\",text_TW=\""+text_TW+"\",text_CN=\""+text_CN+"\"}")
        print("category="+str(category),"index="+str(data_JP[2]),"text_JP="+data_JP[3],"text_TW="+text_TW,"text_CN="+text_CN)
    file.write("\n}\n")

getText(4) # 带昵称的赛马娘名
getText(5) # 赛马娘昵称
getText(6) # 赛马娘名
getText(16) # 歌曲
getText(23) # 材料名
# getText(24) # 材料说明
getText(31) # 马场名
getText(38) # 比赛名
getText(47) # 技能名
# getText(48) # 技能描述
# getText(55) # 特有名词
getText(65) # 称号
getText(75) # 支援卡全名
getText(76) # 支援卡副名
# getText(77) # 支援卡马名
# getText(88) # 赛马娘自我介绍
# getText(92) # 育成事件名
getText(111) # 比赛称号
getText(119) # 剧本全称
getText(130) # 赛马娘称号
# getText(131) # 赛马娘称号获取说明
getText(147) # 因子
# getText(181) # 剧本事件名
file.write("\nreturn p")